package cn.zyq.ykb.service.file.impl;


import cn.zyq.ykb.beans.file.FileStorage;
import cn.zyq.ykb.beans.sys.SysAdmin;
import cn.zyq.ykb.mapper.file.FileStorageMapper;
import cn.zyq.ykb.service.file.FileStorageService;
import cn.zyq.ykb.system.common.BaseService;
import cn.zyq.ykb.system.common.PageParam;
import cn.zyq.ykb.system.enums.core.DeletedTypeEnum;
import cn.zyq.ykb.system.utils.FileConvertUtil;
import cn.zyq.ykb.system.utils.SmartStringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * 文件存储表
 * 2023-05-09 20:58:31
 */

@Slf4j
@Service
public class FileStorageServiceImpl extends BaseService implements FileStorageService {
    @Autowired
    private FileStorageMapper fileStorageMapper;


    @Override
    public PageInfo<FileStorage> list(PageParam pageParam, SysAdmin admin) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        List<FileStorage> dbList = this.fileStorageMapper.list(pageParam.getKv());
        return new PageInfo<>(dbList);
    }

    @Override
    public void add(FileStorage param, SysAdmin admin) {
        this.setCreateBaseData(param, admin);
        this.fileStorageMapper.insertSelective(param);
    }

    @Override
    public void update(FileStorage param, SysAdmin admin) {
        Integer id = param.getFileStorageID();
        this.setUpdateBaseData(param, admin);
        this.fileStorageMapper.updateByPrimaryKeySelective(param);
    }

    @Override
    public void del(String serviceID, SysAdmin admin) {
        FileStorage dbInfo = this.detail(serviceID);
        Integer id = dbInfo.getFileStorageID();
        this.deleteLogic(id, admin, this.fileStorageMapper);
    }

    @Override
    public FileStorage detail(String serviceID, SysAdmin admin) {
        return this.detail(serviceID);
    }

    @Override
    public FileStorage detail(String serviceID) {
        FileStorage dbInfo = this.getByServiceID(serviceID, FileStorage.class, this.fileStorageMapper);
        this.checkLogicExist(dbInfo);
        return dbInfo;
    }

    @Override
    public FileStorage findByKey(String key) {
        FileStorage storage = new FileStorage();
        storage.setKey(key);
        storage.setDeleted(DeletedTypeEnum.NORMAL.getValue());
        return fileStorageMapper.selectOne(storage);
    }

    /**
     * @Description:系统文件在线预览接口
     * @Author: Simon
     */
    public void onlinePreview(String url, HttpServletResponse response) throws Exception {
        //获取文件类型
        String[] str = SmartStringUtil.split(url,"\\.");

        if(str.length==0){
            throw new Exception("文件格式不正确");
        }
        String suffix = str[str.length-1];
        if(!suffix.equals("txt") && !suffix.equals("doc") && !suffix.equals("docx") && !suffix.equals("xls")
                && !suffix.equals("xlsx") && !suffix.equals("ppt") && !suffix.equals("pptx")){
            throw new Exception("文件格式不支持预览");
        }
        InputStream in= FileConvertUtil.convertNetFile(url,suffix);
        OutputStream outputStream = response.getOutputStream();
        //创建存放文件内容的数组
        byte[] buff =new byte[1024];
        //所读取的内容使用n来接收
        int n;
        //当没有读取完时,继续读取,循环
        while((n=in.read(buff))!=-1){
            //将字节数组的数据全部写入到输出流中
            outputStream.write(buff,0,n);
        }
        //强制将缓存区的数据进行输出
        outputStream.flush();
        //关流
        outputStream.close();
        in.close();
    }
}
